<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Add files from a directory by PCRE pattern</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ziparchive.addglob.html">« ZipArchive::addGlob</a></li>
      <li style="float: right;"><a href="ziparchive.clearerror.html">ZipArchive::clearError »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.ziparchive.html">ZipArchive</a></li>
    <li>Add files from a directory by PCRE pattern</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="ziparchive.addpattern" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ZipArchive::addPattern</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8, PECL zip &gt;= 1.9.0)</p><p class="refpurpose"><span class="refname">ZipArchive::addPattern</span> &mdash; <span class="dc-title">Add files from a directory by PCRE pattern</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-ziparchive.addpattern-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>ZipArchive::addPattern</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$pattern</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$path</code><span class="initializer"> = &quot;.&quot;</span></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code><span class="initializer"> = []</span></span>): <span class="type"><span class="type">array</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   Add files from a directory which match the regular expression <code class="parameter">pattern</code>.
   The operation is not recursive. The pattern will be matched against the file name only.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-ziparchive.addpattern-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">pattern</code></dt>

    <dd>

     <p class="para">
      A <a href="book.pcre.html" class="link">PCRE</a> pattern against which files will be matched.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">path</code></dt>

    <dd>

     <p class="para">
      The directory that will be scanned. Defaults to the current working directory.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">options</code></dt>

    <dd>

     <p class="para">
      An associative array of options accepted by <span class="methodname"><a href="ziparchive.addglob.html" class="methodname">ZipArchive::addGlob()</a></span>.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-ziparchive.addpattern-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   An <span class="type">array</span> of added files on success  或者在失败时返回 <strong><code>false</code></strong>
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-ziparchive.addpattern-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="ziparchive.addpattern.example.basic">
   <p><strong>示例 #1 <span class="methodname"><strong>ZipArchive::addPattern()</strong></span> example</strong></p>
   <div class="example-contents"><p>
     Add all php scripts and text files from current directory
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$zip&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ZipArchive</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">open</span><span style="color: #007700">(</span><span style="color: #DD0000">'application.zip'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ZipArchive</span><span style="color: #007700">::</span><span style="color: #0000BB">CREATE&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">ZipArchive</span><span style="color: #007700">::</span><span style="color: #0000BB">OVERWRITE</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">'Failed&nbsp;with&nbsp;code&nbsp;%d'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ret</span><span style="color: #007700">);<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$directory&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">realpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'add_path'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'sources/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'remove_path'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$directory</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addPattern</span><span style="color: #007700">(</span><span style="color: #DD0000">'/\.(?:php|txt)$/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$directory</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-ziparchive.addpattern-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="ziparchive.addfile.html" class="methodname" rel="rdfs-seeAlso">ZipArchive::addFile()</a> - Adds a file to a ZIP archive from the given path</span></li>
    <li class="member"><span class="methodname"><a href="ziparchive.addglob.html" class="methodname" rel="rdfs-seeAlso">ZipArchive::addGlob()</a> - Add files from a directory by glob pattern</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>